package com.algorithm.sort;

import java.util.Objects;

/**
 * 插入排序
 *
 * @program: study
 * @className: InsertSort
 * @description: TODO
 * @author: kukuxiahuni
 * @create: 2019-12-31 14:22
 * @version: v1.0
 **/
public class InsertSort {
    public static void main(String[] args) {
        int[] array = {4, 5, 6, 1, 3, 2};
        insertSort(array);
    }

    /**
     * 功能描述: 插入排序
     *
     * @param:
     * @return:
     * @auther: kukuxiahuni
     * @date: 2019/12/31 2:23 下午
     * @modify_auther: kukuxiahuni
     * @modify_time: 2019/12/31 2:23 下午
     **/
    public static void insertSort(int[] array) {

        if (Objects.isNull(array) || array.length <= 1) {
            return;
        }

        for (int i = 1; i < array.length; ++i) {
            int value = array[i];
            int j = i - 1;

            for (; j >= 0; --j) {
                if (value > array[j]) {
                    break;
                } else {
                    array[j + 1] = array[j];
                }
            }
            array[j + 1] = value;

        }

        for (int num : array) {
            System.out.println(num);
        }

    }
}

